package com.thh.datamonitor.controller;

import java.util.List;

import com.thh.core.manager.DataSourcePoolManager;
import com.thh.datamonitor.dto.TmdDatabaseDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.thh.common.annotation.Log;
import com.thh.common.enums.BusinessType;
import com.thh.datamonitor.entity.TmdDatabase;
import com.thh.datamonitor.service.ITmdDatabaseService;
import com.thh.common.core.controller.BaseController;
import com.thh.common.core.model.AjaxResult;
import com.thh.common.utils.poi.ExcelUtil;
import com.thh.common.core.page.TableDataInfo;

/**
 * 数据库配置Controller
 * 
 * @author thh
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/datamonitor/database")
public class DatabaseController extends BaseController
{
    private String prefix = "datamonitor/database";

    @Autowired
    private ITmdDatabaseService tmdDatabaseService;

    @Autowired
    private DataSourcePoolManager dataSourcePoolManager;

    @RequiresPermissions("datamonitor:database:view")
    @GetMapping()
    public String database()
    {
        return prefix + "/database";
    }

    @PostMapping("/popuplist")
    @ResponseBody
    public TableDataInfo popuplist(TmdDatabase tmdDatabase)
    {
        startPage();
        List<TmdDatabase> list = tmdDatabaseService.selectTmdDatabaseList(tmdDatabase);
        return getDataTable(list);
    }

    /**
     * 查询数据库配置列表
     */
    @RequiresPermissions("datamonitor:database:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdDatabase tmdDatabase)
    {
        startPage();
        List<TmdDatabase> list = tmdDatabaseService.selectTmdDatabaseList(tmdDatabase);
        return getDataTable(list);
    }

    /**
     * 导出数据库配置列表
     */
    @RequiresPermissions("datamonitor:database:export")
    @Log(title = "数据库配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdDatabase tmdDatabase)
    {
        List<TmdDatabase> list = tmdDatabaseService.selectTmdDatabaseList(tmdDatabase);
        ExcelUtil<TmdDatabase> util = new ExcelUtil<TmdDatabase>(TmdDatabase.class);
        return util.exportExcel(list, "数据库配置数据");
    }

    /**
     * 新增数据库配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据库配置
     */
    @RequiresPermissions("datamonitor:database:add")
    @Log(title = "数据库配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdDatabaseDto tmdDatabaseDto)
    {
        return toAjax(tmdDatabaseService.insertTmdDatabase(tmdDatabaseDto));
    }

    /**
     * 修改数据库配置
     */
    @RequiresPermissions("datamonitor:database:edit")
    @GetMapping("/edit/{databaseId}")
    public String edit(@PathVariable("databaseId") Long databaseId, ModelMap mmap)
    {
        TmdDatabase tmdDatabase = tmdDatabaseService.selectTmdDatabaseByDatabaseId(databaseId);
        mmap.put("tmdDatabase", tmdDatabase);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库配置
     */
    @RequiresPermissions("datamonitor:database:edit")
    @Log(title = "数据库配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdDatabaseDto tmdDatabaseDto)
    {
        return toAjax(tmdDatabaseService.updateTmdDatabase(tmdDatabaseDto));
    }

    /**
     * 删除数据库配置
     */
    @RequiresPermissions("datamonitor:database:remove")
    @Log(title = "数据库配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdDatabaseService.deleteTmdDatabaseByDatabaseIds(ids));
    }


    /**
     * 数据库连接测试
     */
    @PostMapping(path = "/testConnection")
    @ResponseBody
    public AjaxResult testConnection(TmdDatabaseDto tmdDatabaseDto) throws Exception {
        tmdDatabaseService.testConnection(tmdDatabaseDto);
        return success("连接成功！");
    }
}
